/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.main.action;

import com.documents.common.core.PageBean;
import com.documents.common.core.action.BaseActionSupport;
import com.documents.pojo.Notice;
import com.documents.pojo.Users;
import com.documents.service.NoticeService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Administrator
 */
public class NoticeAction extends BaseActionSupport implements ServletRequestAware {

    private Notice notice;
    private NoticeService noticeService;
    private int notice_id;
    private int page = 1;
    private PageBean pageBean;
    private int pageSize = 20;
    private String authorName;
    private String titleName;

    public String getTitleName() {
        return titleName;
    }

    public void setTitleName(String titleName) {
        this.titleName = titleName;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Notice getNotice() {
        return notice;
    }

    public void setNotice(Notice notice) {
        this.notice = notice;
    }

    public NoticeService getNoticeService() {
        return noticeService;
    }

    public void setNoticeService(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    public int getNotice_id() {
        return notice_id;
    }

    public void setNotice_id(int notice_id) {
        this.notice_id = notice_id;
    }

    public String add() throws Exception {
        if (notice != null) {
            noticeService.save(notice);
        }
        return SUCCESS;

    }

    public String delete() throws Exception {

        noticeService.delById(notice.getId());
        return SUCCESS;
    }
    // 3.更新之前先取出来;

    public String updateSystemNotice() throws Exception {
        if (notice != null) {
            noticeService.update(notice);
        }
        return SUCCESS;
    }

    public String show() throws Exception {
        if (notice.getId() > 0) {
            notice = noticeService.get(notice.getId());
            if (notice.getType() == 1) {
                titleName = "公告";
            } else {
                titleName = "资讯";
            }
            return SUCCESS;
        } else {
            return null;
        }


    }

    public String list() throws Exception {
        if (notice!=null&&notice.getType() == 1) {
            titleName = "公告";
        } else {
            titleName = "资讯";
        }
        pageBean = noticeService.getPageBean(page, pageSize, notice);

        return SUCCESS;
    }

    public String addInput() throws Exception {
        Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
        if (users != null) {
            authorName = users.getName();
        }
        return SUCCESS;
    }

    public String updateInput() throws Exception {

        notice = this.noticeService.get(notice.getId());
        if (notice == null) {
            return ERROR;
        } else {

            return SUCCESS;
        }

    }
}
